package com.itheima.sc.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.itheima.sc.api.sms.SmsFlashPromotionSessionApi;
import com.itheima.sc.pojo.sms.FlashPromotionSession;
import com.itheima.sc.config.ComException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SmsFlashSessionService {

    @DubboReference
    private SmsFlashPromotionSessionApi sessionApi;
    @Autowired
    private SmsFlashProductRelationService productRelationService;

    //添加场次
    public void addFlashSession(FlashPromotionSession promotionSession) {
        promotionSession.setCreateTime(new Date(System.currentTimeMillis()));
        //远端调用api
        sessionApi.addFlashSession(promotionSession);
    }

    //删除场次信息
    public void deleteFlashSession(Long id) {
        //获取场次的状态
       int status= sessionApi.findStatus(id);
        if (status== 1) {
            throw new ComException("启用状态,无法删除");
        }
        sessionApi.deleteFlashSession(id);
    }

    //获取全部场次信息的信息
    public List<FlashPromotionSession> getFlashSessionList() {
        return sessionApi.getFlashSessionList();
    }

    //获取全部可选场次及其数量
    public List<FlashPromotionSession> getFlashSessionSelectList(Integer flashPromotionId) {
        //获取商品种类的数量
        List<FlashPromotionSession> flashSessionSelectList = sessionApi.getFlashSessionSelectList(flashPromotionId);
        if (CollUtil.isNotEmpty(flashSessionSelectList)) {
            for (FlashPromotionSession session : flashSessionSelectList) {
                int count = productRelationService.findRelation(session.getId());
                session.setProductCount(count);
            }
            return flashSessionSelectList;
        }
            return null;
    }

    //修改启用状态
    public void updateFlashSessionStatus(Long id, Integer status) {
        sessionApi.updateFlashSessionStatus(id, status);
    }

    //修改场次
    public void updateFlashSession(Long id, FlashPromotionSession session) {
        sessionApi.updateFlashSession(session);
    }

    //查询场次详情
    public FlashPromotionSession selectFlashSessionInfo(Long id) {
        return sessionApi.selectFlashSessionInfo(id);
    }
}
